package com.gin.kotlin.no_gay_alliance.activity.pm

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.text.InputType
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.EditText
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.databinding.ActivityPrivateMessageNewBinding
import com.gin.nga.callback.NgaCallback
import com.gin.nga.response.base.NgaResponse
import retrofit2.Call
import retrofit2.Response

class PrivateMessageNewActivity : AbstractBindingActivity<ActivityPrivateMessageNewBinding>(), NgaCallback<List<String>> {
    private var users: ArrayList<String>? = null

    override fun onCreate() {
    }

    override fun handleIntent(intent: Intent) {
        this.users = intent.getStringArrayListExtra(KEY_ARGUMENT)
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.private_message_new, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            // 修改邀请用户
            R.id.mi_private_message_user_new -> {
                val editText = EditText(this)
                editText.maxLines = 5
                editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                editText.setText(this.users?.joinToString(" "))

                AlertDialogUtils.builder(
                    this,
                    getString(R.string.dialog_title_pm_invite_user),
                    getString(R.string.dialog_message_invite_user),
                    editText
                ) { _, which ->
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        this.users = arrayListOf(* editText.text.toString().split(" ").map { it.trim() }.filter { it.isNotEmpty() }.toTypedArray())
                    }
                }.show()
            }

            R.id.mi_send_post -> {
                val b = this.b ?: return false
                val title = b.etTitle.text.toString()
                val content = b.etContent.text.toString()
                val users = this.users
                // 表单校验非空
                if (users.isNullOrEmpty()) {
                    toast(getString(R.string.toast_message_users_can_not_be_empty))
                    return true
                }
                if (title.isEmpty()) {
                    toast(getString(R.string.toast_message_title_can_not_be_empty))
                    return true
                }
                if (content.isEmpty()) {
                    toast(getString(R.string.toast_message_content_can_not_be_empty))
                    return true
                }
                // 发送请求
                App.INSTANCE.ngaApi.messageApi.new(title, content, users).enqueue(this)
            }
        }

        return super.onOptionsItemSelected(item)
    }

    override fun onSuccess(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, data: List<String>?) {
        data?.get(0)?.also { toast(it) }
        setResult(200)
        finish()
    }

    override fun onError(call: Call<NgaResponse<List<String>>>, response: Response<NgaResponse<List<String>>>, body: NgaResponse<List<String>>?, errorMessage: String) {
        super.onError(call, response, body, errorMessage)
        toastLong(errorMessage)
    }

    companion object {
        const val KEY_ARGUMENT = "USERS"

        @JvmStatic
        fun start(context: Context, list: ArrayList<String>? = null) = context.startActivity(obtainIntent(context, list))

        @JvmStatic
        fun obtainIntent(context: Context, list: ArrayList<String>?) = Intent(context, PrivateMessageNewActivity::class.java).putStringArrayListExtra(KEY_ARGUMENT, list)
    }
}